package ro.msg.office.entities;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import ro.msg.office.ticketmanager.DO.TicketDO;


@Entity
@Table(name="TB_TICKET")
@SequenceGenerator(name="TICKET_SEQUENCE_GENERATOR", 
			sequenceName="TICKET_SEQUENCE", 
			initialValue=6, allocationSize=1)
/******************************************
   Query for:
 *findUserById
 *findAllById
 *finAllByUser
 *findAllByDepartment
 ******************************************/
@NamedQueries({
	@NamedQuery(name="Ticket.findUserById",query="SELECT t.user FROM Ticket t WHERE t.ticketId = :id"),
	@NamedQuery(name="Ticket.findAllById",query="SELECT t FROM Ticket t WHERE t.ticketId = :id"),
	@NamedQuery(name="Ticket.findAllByUser",query="SELECT t FROM Ticket t  WHERE t.user = :user"),
	@NamedQuery(name="Ticket.findAllByDepartment",query=" SELECT t FROM Ticket t WHERE t.user.department.departmentId = :department")

})

public class Ticket implements Serializable {


	private static final long serialVersionUID = 1L;


	/******************************************
	   Generate autoincrement for Id
	 ******************************************/

	@Id
	@GeneratedValue(strategy=GenerationType.SEQUENCE,
			generator="TICKET_SEQUENCE_GENERATOR")
	@Column(name="ticketId")
	private Long ticketId;

	/******************************************
	   ManyToOne relationship 
	 ******************************************/
	@ManyToOne
	@JoinColumn(name = "userId")
	private User user;


	@Column(name="creationDate")
	@Temporal(TemporalType.TIMESTAMP)
	private Date creationDate;

	@Column(name="approvalDate")
	@Temporal(TemporalType.TIMESTAMP)
	private Date approvalDate;

	@Column(name="startTime")
	@Temporal(TemporalType.TIMESTAMP)
	private Date startTime;

	@Column(name="endTime")
	@Temporal(TemporalType.TIMESTAMP)
	private Date endTime;

	@Column(name="description")
	private String description;

	@Column(name="status")
	private Long  status;

	@Column(name="rejected")
	private String rejected;

	public Ticket(){
		super();
	}

	public Ticket(Long ticketId, User user, Date creationDate, Date startTime, Date endTime, String description){
		this.ticketId = ticketId;
		this.user = user;
		this.creationDate = creationDate;
		this.startTime = startTime;
		this.endTime = endTime;
		this.description = description;
	}

	public Long getTicketId() {
		return ticketId;
	}

	public void setTicketId(Long ticketId) {
		this.ticketId = ticketId;
	}


	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}


	public Date getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	public Date getApprovalDate() {
		return approvalDate;
	}

	public void setApprovalDate(Date approvalDate) {
		this.approvalDate = approvalDate;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}


	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}


	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}


	public Long getStatus() {
		return status;
	}

	public void setStatus(Long status) {
		this.status = status;
	}


	public String getRejected() {
		return rejected;
	}

	public void setRejected(String rejected) {
		this.rejected = rejected;
	}

	/**
	 * Map Ticket to TicketDO
	 * @param Ticket
	 * @return TicketDO
	 */
	public TicketDO mapTicketToDO(Ticket t){
		TicketDO ticketDO = new TicketDO();
		ticketDO.setApprovalDate(t.getApprovalDate());
		ticketDO.setCreationDate(t.getCreationDate());
		ticketDO.setDescription(t.getDescription());
		ticketDO.setEndTime(t.getEndTime());
		ticketDO.setRejected(t.getRejected());
		ticketDO.setStartTime(t.getStartTime());
		ticketDO.setStatus(t.getStatus());
		ticketDO.setTicketId(t.getTicketId());
		ticketDO.setUserID(user.getUserId());
		ticketDO.setUserName(user.getName());
		return ticketDO;

	}
}
